var config = require('../../script/config');
var http = require('../../script/api.js')
// import tool from '../../utils/common.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    CommonParam: '',
    userinfo: null,
    pagination: {
      page: 1,
      rows: 10,
      sidx: '',
      sord: ''
    },
    count: 0,
    orderList: [],
    canLoadData: true,
    noData: false,
    isReachBottom: false,
  },
  _refreshData: function () {
    this.setData({ //初始化数据
      pagination: {
        page: 1,
        rows: 10,
        sidx: '',
        sord: ''
      },
      count: 0,
      canLoadData: true,
      noData: false,
      isReachBottom: false,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getUserInfo()
    this._refreshData();
    this.GetList(true)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    this._refreshData();
    this.GetList(true)
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      isReachBottom: true
    });
    if (this.data.canLoadData) {
      this.setData({
        'pagination.page': this.data.pagination.page + 1
      })
      this.GetList();
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none',
        duration: 1000
      });
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  call: function (e) {
    var phoneNumber = e.currentTarget.dataset.phone
    var res = wx.getSystemInfoSync();
    if (res.platform == 'ios') {
      wx.makePhoneCall({
        phoneNumber: phoneNumber
      });
    } else {
      wx.showActionSheet({
        itemList: [phoneNumber, '呼叫'],
        success: function (res) {
          if (res.tapIndex == 1) {
            wx.makePhoneCall({
              phoneNumber: phoneNumber
            });
          }
        }
      })
    }
  },
  getUserInfo: function () {
    try {
      var value = wx.getStorageSync(config.storageKey.userinfo)
      if (value) {
        this.setData({
          userinfo: value
        });
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  searchFocus: function (e) {
    wx.navigateTo({
      url: "../search/search?IsFinish=1"
    })
  },
  GetList(isRefresh) {
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    let params = {
      pagination: {
        ...this.data.pagination
      },
      queryParam: {
        IsFinish: true,
      }
    };
    http.GetOrderList(params).then(res => {
      if (res.code === 200) {
        let dataList = [];
        for (var i = 0; i < res.data.Items.length; i++) {
          res.data.Items[i].btnStatus = 0;
        }
        if (isRefresh) {
          dataList = [...res.data.Items];
        } else {
          dataList = [...this.data.orderList, ...res.data.Items];
        }
        let canLoadData = res.data.HasNextPages;
        const noData = dataList.length === 0;
        this.setData({
          orderList: dataList,
          count: res.data.TotalCount,
          canLoadData: canLoadData,
          noData: noData
        });
      }
      wx.hideLoading();
    }).catch(_err => {
      wx.hideLoading();
    });
  },
  toDetail: function (e) {
    const WorkID = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../order-detail/order-detail?WorkID=" + WorkID
    });
  }
})